﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Sohar.Core
{
	public class WordQuestionaryTimer
	{
		private int timeout;
		private Timer timer;
		private WordQuestion currentQuestion;

		public event EventHandler<WordQuestionaryTimerEventArgs> Timeout;

		public WordQuestionaryTimer(int timeout)
		{
			this.timeout = timeout;
			this.timer = new Timer(this.timeout * 1000);
			this.timer.Elapsed += new ElapsedEventHandler(OnElapsed);
		}

		public void Next(WordQuestion question)
		{
			if (this.currentQuestion != null)
				this.timer.Stop();

			this.currentQuestion = question;
			this.timer.Start();
		}

		private void OnElapsed(object sender, ElapsedEventArgs e)
		{
			this.timer.Stop();

			if (Timeout != null)
				Timeout(this, new WordQuestionaryTimerEventArgs(this.currentQuestion));

			this.currentQuestion = null;
		}
	}

	public class WordQuestionaryTimerEventArgs : EventArgs
	{
		public WordQuestionaryTimerEventArgs(WordQuestion question)
		{
			this.Question = question;
		}

		public WordQuestion Question
		{
			get;
			private set;
		}
	}
}
